package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqPollingProject;
import com.ysd.lis.entity.eq.EqPollingStandard;
import com.ysd.lis.entity.lab.LabSupplier;
import com.ysd.lis.entity.lab.LabSupplierAppraise;
import com.ysd.lis.mapper.eq.EqPollingProjectMapper;
import com.ysd.lis.mapper.eq.EqPollingStandardMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.PollingStandardDto;
import com.ysd.lis.service.eq.EqPollingStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-07-31
 */
@Service
public class EqPollingStandardServiceImpl extends ServiceImpl<EqPollingStandardMapper, EqPollingStandard> implements EqPollingStandardService {

    @Autowired
    EqPollingStandardMapper eqPollingStandardMapper;
    @Autowired
    EqPollingProjectMapper eqPollingProjectMapper;
    @Override
    public Result findPollingStandardList(CommonPageDto param) {
        MPJLambdaWrapper<EqPollingStandard> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqPollingStandard.class);
        lambdaQueryWrapper.selectCollection(EqPollingProject.class,EqPollingStandard::getPollingProjectList);
        lambdaQueryWrapper.leftJoin(EqPollingProject.class,EqPollingProject::getStandardId,EqPollingProject::getId);
        lambdaQueryWrapper.eq(EqPollingStandard::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqPollingStandard::getStandardName, param.getSeachValue()).or().like(EqPollingStandard::getStandardCode, param.getSeachValue()));
        }
        List<EqPollingStandard> eqPollingStandards = eqPollingStandardMapper.selectJoinList(EqPollingStandard.class,lambdaQueryWrapper);
        return Result.succ(1, "查询成功", eqPollingStandards);
    }

    @Override
    public Result addPollingStandard(EqPollingStandard standard) {
        int insert = eqPollingStandardMapper.insert(standard);
        if(insert>0){
            return Result.succ(1, "添加成功", standard);
        }else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editPollingStandard(EqPollingStandard standard) {
        int updateById = eqPollingStandardMapper.updateById(standard);
        if(updateById>0){
            return Result.succ(1, "修改成功", standard);
        }else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result batchDeletePollingStandardList(PollingStandardDto dto) {
        List<EqPollingStandard> pollingStandardList = dto.getPollingStandardList();
        List<String> ids = pollingStandardList.stream().map(EqPollingStandard::getId).collect(Collectors.toList());
        //查询标准下是否有项目
        LambdaQueryWrapper<EqPollingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqPollingProject::getDelFlag,0)
                .in(EqPollingProject::getStandardId,ids);
        Integer cnt = eqPollingProjectMapper.selectCount(queryWrapper);
        if(cnt>0){
            return Result.succ(1, "标准下有项目，不能删除", null);
        }

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
